package com.service.impl;

import com.entity.Items;
import com.entity.Order;
import com.github.pagehelper.PageInfo;
import com.mapper.ItemsMapper;
import com.mapper.OrderMapper;
import com.service.ItemsService;
import com.service.OrderService;
import com.vo.CartItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.Date;
import java.util.List;

@Service("orderService")
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ItemsMapper itemsMapper;
    @Override
    public void insertOrder(Order order, Collection<CartItem> items) {
        orderMapper.insertOrder(order);
        for(CartItem cartItem:items){
            Items item = new Items();
            item.setBook_id(cartItem.getBook().getBook_id());
            item.setCount(cartItem.getCount());
            item.setCreate_date(new Date());
            item.setOrder_id(order.getOrder_id());
            itemsMapper.insertItems(item);
        }
    }

    @Override
    public void deleteOrderById(Integer id) {
        orderMapper.deleteOrderById(id);
    }

    @Override
    public void updateOrder(Order order) {
        orderMapper.updateOrder(order);
    }

    @Override
    public Order selectOrderById(Integer id) {
        return orderMapper.selectOrderById(id);
    }

    @Override
    public Order selectOrderByName(String name) {
        return orderMapper.selectOrderByName(name);
    }

    @Override
    public List<Order> selectOrder() {
        return orderMapper.selectOrder();
    }

    @Override
    public PageInfo<Order> selectPageOrder(Integer pageNum, Integer pageSize) {
        return new PageInfo<>(orderMapper.selectPageOrder(pageNum,pageSize));
    }
}
